#include <iostream>
#include <vector>
#include <shared_mutex>
#include <mutex>
#include <thread>

namespace shy
{
class CalArray
{
private:
    int _N; // num
    int _M; // worker
    int _num_work;
    std::vector<int> _S;
    std::vector<std::mutex> _locks;

public:
    CalArray(const int WorkerNum)
        : _N(100000),
          _M(WorkerNum), // M >=2
          _num_work(10000),
          _S(100000),
          _locks(100000)
    {
    }

    void Init()
    {
        for (auto &it : _S)
        {
            it = rand() % 10;
        }
    }

    void work()
    {
        for (int it = 0; it < _num_work; it++)
        {
            int i = rand() % _N;
            int j = rand() % _N;

            // get lock
            int lock1 = std::min(i, std::min((i + 1) % _N, (i + 2) % _N));
            int lock2 = std::max(std::min(i, (i + 1) % _N), std::min(std::max(i, (i + 1) % _N), (i + 2) % _N));
            int lock3 = std::max(i, std::max((i + 1) % _N, (i + 2) % _N));

            _locks[lock1].lock();
            _locks[lock2].lock();
            _locks[lock3].lock();

            // calculate
            int sum = _S[i] + _S[(i + 1) % _N] + _S[(i + 2) % _N];

            // free lock
            _locks[lock1].unlock();
            _locks[lock2].unlock();
            _locks[lock3].unlock();

            // get lock
            _locks[j].lock();

            // update S[j]
            _S[j] = sum;

            // free lock
            _locks[j].unlock();
        }
    }

    void run()
    {
        std::vector<std::thread> threads;

        for (int i = 0; i <_M; i++)
        {
            threads.emplace_back(&CalArray::work, this);
        }

        for (auto &t : threads)
        {
            t.join();
        }
    }

    void Print(int num)
    {
        for (int i = 0; i < num; i++)
        {
            std::cout <<_S[i] << " ";
        }
        std::cout << std::endl;
    }

    ~CalArray()
    {
    }
};
}
int main()
{
int M = 0;
std::cin >> M;
if (M < 2)
{
std::cerr << "workersnum error!" << std::endl;
return -1;
}

shy::CalArray arr(M);
arr.Init();
arr.run();
arr.Print(1000);
}

